package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.service.PaymentHystrixService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Tu
 * @date 2023/5/1 15:23
 */
@Slf4j
@RestController
@RequestMapping("/api/v1")
@DefaultProperties(defaultFallback = "defaultFallBackHandler")
public class OrderHystrixController {

    @Autowired
    private PaymentHystrixService paymentHystrixService;

    @GetMapping("/order/hystrix/ok/{id}")
    public String paymentInfoSuccess(@PathVariable Integer id) {
        String result = paymentHystrixService.paymentInfoSuccess(id);
        log.info("successResult：" + result);
        return result;
    }

    @GetMapping("/order/hystrix/error/{id}")
    /*@HystrixCommand(fallbackMethod = "paymentInfoErrorHandler", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1500")})*/
    //注意这里HystrixCommand注解还是要加的，不加代表不走降级
    @HystrixCommand
    public String paymentInfoError(@PathVariable Integer id) {
        //int age = 10 / 0;
        String result = paymentHystrixService.paymentInfoError(id);
        log.info("successResult：" + result);
        return result;
    }

    public String paymentInfoErrorHandler(Integer id) {
        return "消费端调用支付模块异常，请稍后再试！线程池：" + Thread.currentThread().getName() + "，id：" + id;
    }

    /**
     * 默认降级服务类，发生错误时，如果没有配置特殊的降级方法，则会进入默认降级方法
     *
     * @return
     */
    public String defaultFallBackHandler() {
        return "系统异常，请稍后再试！线程池：" + Thread.currentThread().getName();
    }

}
